package com.example.travel_front.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.travel_front.Model.PopularPlace;
import com.example.travel_front.R;

import java.util.List;

public class PopularPlaceAdapter extends RecyclerView.Adapter<PopularPlaceAdapter.PopularPlaceViewHolder> {
    private final List<PopularPlace> popularPlacesList;

    // 构造方法，传入数据
    public PopularPlaceAdapter(List<PopularPlace> popularPlacesList) {
        this.popularPlacesList = popularPlacesList;
    }

    @NonNull
    @Override
    public PopularPlaceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 创建视图并返回ViewHolder
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_popular_place, parent, false);
        return new PopularPlaceViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PopularPlaceViewHolder holder, int position) {
        // 绑定数据到ViewHolder
        PopularPlace place = popularPlacesList.get(position);
        holder.titleTextView.setText(place.getTitle());
        holder.txtLikes.setText(place.getLikes() + " likes");
        Glide.with(holder.imageView.getContext()).load(place.getImageUrl()).error(R.drawable.ic_popplace).into(holder.imageView);

    }

    @Override
    public int getItemCount() {
        return popularPlacesList.size();
    }

    static class PopularPlaceViewHolder extends RecyclerView.ViewHolder {
        // 定义视图
        ImageView imageView;
        TextView titleTextView;
        // ... 可以添加更多视图
        TextView txtLikes;

        public PopularPlaceViewHolder(@NonNull View itemView) {
            super(itemView);
            // 初始化视图
            imageView = itemView.findViewById(R.id.imgPopularPlace);
            titleTextView = itemView.findViewById(R.id.txtPopularPlaceTitle);
            txtLikes = itemView.findViewById(R.id.txtPopularPlaceLikes);
        }
    }
}

